package vn.vnu.uet.university;

import java.util.ArrayList;
import java.util.HashMap;

import vn.vnu.uet.university.MyHorizontalScrollView.SizeCallback;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DefaultActivity extends Activity {
	MyHorizontalScrollView scrollView;
    View menu;
    View app;
    
    ImageView btnSlide;
    boolean menuOut = false;
    Handler handler = new Handler();
    int btnWidth;
	ListView slideMenu;

	protected void onCreate(Bundle savedInstanceState, int main) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = LayoutInflater.from(this);
        scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
        setContentView(scrollView);

        menu = inflater.inflate(R.layout.horz_scroll_menu, null);
        app = inflater.inflate(main, null);
        ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

        btnSlide = (ImageView) tabBar.findViewById(R.id.BtnSlide);
        btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));

        final View[] children = new View[] { menu, app };

        // Scroll to app (view[1]) when layout finished.
        int scrollToViewIdx = 1;
        scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));
        
		slideMenu = (ListView) findViewById(R.id.bookmarks_list);

		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
		
		map = new HashMap<String, String>();
		map.put("menu_item_text", Token.instance().user.fullName);
		map.put("menu_item_icon", String.valueOf(R.drawable.account_default));
		listItem.add(map);

		map = new HashMap<String, String>();
		map.put("menu_item_text", "Trang chủ");
		map.put("menu_item_icon", String.valueOf(R.drawable.card_default));
		listItem.add(map);

		map = new HashMap<String, String>();
		map.put("menu_item_text", "Tin tức");
		map.put("menu_item_icon", String.valueOf(R.drawable.transfer_default));
		listItem.add(map);
		
		map = new HashMap<String, String>();
		map.put("menu_item_text", "Thông báo");
		map.put("menu_item_icon", String.valueOf(R.drawable.payment_default));
		listItem.add(map);
		
		map = new HashMap<String, String>();
		map.put("menu_item_text", "Xem điểm");
		map.put("menu_item_icon", String.valueOf(R.drawable.topup_default));
		listItem.add(map);
		
		map = new HashMap<String, String>();
		map.put("menu_item_text", "Thời khoá biểu");
		map.put("menu_item_icon", String.valueOf(R.drawable.saving_default));
		listItem.add(map);

		SimpleAdapter mSchedule = new SimpleAdapter(DefaultActivity.this,
				listItem, R.layout.menu_item, new String[] { "menu_item_icon",
						"menu_item_text" }, new int[] { R.id.menu_item_icon,
						R.id.menu_item_text });
		slideMenu.setAdapter(mSchedule);
		
		slideMenu.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
//				AlertDialog.Builder alertbox = new Builder(DefaultActivity.this);
				btnSlide.performClick();
				switch (position) {
				case 0:
					ViewController.showAccountDetail(DefaultActivity.this);
					break;
				case 1:
					ViewController.showMainActivity(DefaultActivity.this);
					break;
				case 2:
					ViewController.showNews(DefaultActivity.this);
					break;
				case 3:
					ViewController.showReport(DefaultActivity.this);
					break;
				case 4:
					ViewController.showScore(DefaultActivity.this);
					break;
				case 5:
					ViewController.showSchedule(DefaultActivity.this);
					break;
				default:
					break;
				}
				
			}
		});
		
	}

	/**
	 * Helper for examples with a HSV that should be scrolled by a menu View's
	 * width.
	 */
	static class ClickListenerForScrolling implements OnClickListener {
		HorizontalScrollView scrollView;
		View menu;
		/**
		 * Menu must NOT be out/shown to start with.
		 */
		boolean menuOut = false;

		public ClickListenerForScrolling(HorizontalScrollView scrollView,
				View menu) {
			super();
			this.scrollView = scrollView;
			this.menu = menu;
		}

		@Override
		public void onClick(View v) {
			int menuWidth = menu.getMeasuredWidth();

			// Ensure menu is visible
			menu.setVisibility(View.VISIBLE);

			if (!menuOut) {
				// Scroll to 0 to reveal menu
				int left = 0;
				scrollView.smoothScrollTo(left, 0);
			} else {
				// Scroll to menuWidth so menu isn't on screen.
				int left = menuWidth;
				scrollView.smoothScrollTo(left, 0);
			}
			menuOut = !menuOut;
		}
	}

	/**
	 * Helper that remembers the width of the 'slide' button, so that the
	 * 'slide' button remains in view, even when the menu is showing.
	 */
	static class SizeCallbackForMenu implements SizeCallback {
		int btnWidth;
		View btnSlide;

		public SizeCallbackForMenu(View btnSlide) {
			super();
			this.btnSlide = btnSlide;
		}

		@Override
		public void onGlobalLayout() {
			btnWidth = btnSlide.getMeasuredWidth();
			System.out.println("btnWidth=" + btnWidth);
		}

		@Override
		public void getViewSize(int idx, int w, int h, int[] dims) {
			dims[0] = w;
			dims[1] = h;
			final int menuIdx = 0;
			if (idx == menuIdx) {
				dims[0] = w - btnWidth;
			}
		}
	}
	
	void setTitle(String title) {       
        TextView tv = (TextView) findViewById(R.id.title);
        tv.setText(title);
	}
}
